package cn.iune.erp.inventory;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;
@Transactional
@Service
public class FileUnitServiceImpl implements IFileUnitService {

	@Autowired
	private IFileUnitDao dao;
	
//	private final Logger log = LoggerFactory.getLogger(FileUnitUtil.class);
	
	public FileUnitServiceImpl(){
//		dao = new FileUnitDaoJPAImpl();
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IFileUnitService#getAll()
	 */
	@Override
	public List<FileUnit> getAll(){
		return dao.getAll();
	}
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IFileUnitService#getByID(java.lang.String)
	 */
	@Override
	public FileUnit getByID(String fID) {
		return dao.getByID(fID);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IFileUnitService#delete(java.lang.String)
	 */
	@Transactional
	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}

	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IFileUnitService#getByNo(java.lang.String, java.lang.String)
	 */
	@Override
	public FileUnit getByNo(String unitCode, String enterpriseNo) {
		return dao.getByNo(unitCode, enterpriseNo);
	}

	/*
	public JSONObject query(JSONObject data) throws Throwable {
		
		// 获取参数
		Object columns = data.getJSONObject("columns"); // 列定义
		Integer limit = data.getInteger("limit"); // 分页查询的行数
		Integer offset = data.getInteger("offset"); // 分页查询的行偏移
		String search = data.getString("search"); // 检索关键字
		String filter = data.getString("filter"); // 过滤条件
		String id = data.getString("id");

		
		// 存放SQL中的参数值
		List<Object> sqlParams = new ArrayList<Object>();
		// 存放SQL中的过滤条件
		List<String> filters = new ArrayList<String>();
		if (!CommonService.isEmptyString(search)) {
			// 增加过滤条件
			filters.add("unitCode LIKE ? OR unitName LIKE ? OR unitType LIKE ?");
			// 检索关键字中如果没有%，则前后自动加%
			search = (search.indexOf("%") != -1) ? search : "%" + search + "%";
			// sqlParams的参数个数和顺序必须与过滤条件的?相匹配 
			for (int i = 0; i < 3; i++) {
				sqlParams.add(search);
			}
		}
		//处理过滤条件
		if (!CommonService.isEmptyString(filter)) {
			filters.add(filter);
		}
		//处理id
		if (!CommonService.isEmptyString(id)) {
			filters.add("fID='"+id+"'");
		}
	
		
		Table table = Transform.createTableByColumnsDefine((JSONObject) columns);
		
		if (offset != null && offset.equals(0)) {
			long rowCount = dao.getCount(filters, sqlParams);
			table.setTotal((int)rowCount);
		}

		String orderBy = "cDatetime DESC";
				
		//获取数据
		List<Row> rows = listToRows(dao.query(filters,orderBy,sqlParams,offset,limit));
	    table.appendRows(rows);
		JSONObject json = Transform.tableToJson(table);
	
		return json;
	}


	public int save(JSONObject object) throws Throwable {
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		List<FileUnit> list = rowsToList(table.getRows());
		for(FileUnit obj : list){
			if(RowState.NEW.equals(obj.getRowState())){
				obj.setcUsercode(SecurityService.getCurrentPerson().getsCode());
				obj.setcUsername(SecurityService.getCurrentPerson().getsName());
				obj.setcDatetime(CommonService.getCurrentDateTime());
				dao.insert(obj);
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				obj.seteUsercode(SecurityService.getCurrentPerson().getsCode());
				obj.seteUsername(SecurityService.getCurrentPerson().getsName());
				obj.seteDatetime(CommonService.getCurrentDateTime());
				dao.update(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
		}
		return 0;
	}

	public List<Row> listToRows(List<FileUnit> list){
		//添加行集
		List<Row> rows = new ArrayList<Row>();
		//循环行
	    for(FileUnit o : list){
	    	rows.add(objectToRow(o));
	    }
	    return rows;
	}

	public List<FileUnit> rowsToList(List<Row> rows){
		//添加行集
		List<FileUnit> list = new ArrayList<FileUnit>();
		//循环行
	    for(Row r : rows){
	    	list.add(rowToObject(r));
	    }
	    return list;
	}

	public Row objectToRow(FileUnit obj){
    	
		Map<String, ColumnValue> values = new HashMap<String, ColumnValue>();
		//循环行中每一列
		ColumnValue value = new ColumnValue(obj.getfID());
		values.put("fID", value);
		value = new ColumnValue(obj.getSystemCode());
 		values.put("systemCode", value);
		value = new ColumnValue(obj.getBillType());
 		values.put("billType", value);
		value = new ColumnValue(obj.getBillTypeName());
 		values.put("billTypeName", value);
		value = new ColumnValue(obj.getOrderDate());
 		values.put("orderDate", value);
		value = new ColumnValue(obj.getState());
 		values.put("state", value);
		value = new ColumnValue(obj.getStatus());
 		values.put("status", value);

		value = new ColumnValue(obj.getUnitCode());
 		values.put("unitCode", value);
		value = new ColumnValue(obj.getUnitName());
 		values.put("unitName", value);
		value = new ColumnValue(obj.getUnitType());
 		values.put("unitType", value);

		value = new ColumnValue(obj.getComments());
 		values.put("comments", value);

		value = new ColumnValue(obj.getcUsercode());
 		values.put("cUsercode", value);
		value = new ColumnValue(obj.getcUsername());
 		values.put("cUsername", value);
		value = new ColumnValue(obj.getcDatetime());
 		values.put("cDatetime", value);
		value = new ColumnValue(obj.gettUsercode());
 		values.put("tUsercode", value);
		value = new ColumnValue(obj.gettUsername());
 		values.put("tUsername", value);
		value = new ColumnValue(obj.gettDatetime());
 		values.put("tDatetime", value);
		value = new ColumnValue(obj.geteUsercode());
 		values.put("eUsercode", value);
		value = new ColumnValue(obj.geteUsername());
 		values.put("eUsername", value);
		value = new ColumnValue(obj.geteDatetime());
 		values.put("eDatetime", value);

		value = new ColumnValue(obj.getsCreatorFID());
 		values.put("sCreatorFID", value);

		value = new ColumnValue(obj.getUserId());
 		values.put("userId", value);
		value = new ColumnValue(obj.getEnterpriseNo());
 		values.put("enterpriseNo", value);
		value = new ColumnValue(obj.getEnterpriseName());
 		values.put("enterpriseName", value);

		value = new ColumnValue(obj.getVersion());
 		values.put("version", value);
		
		Row row = new Row(values,RowState.NONE);
		return row;
	}
	
	public FileUnit rowToObject(Row row){
		FileUnit obj = new FileUnit();
		obj.setfID(row.getString("fID"));
	    
		obj.setSystemCode(row.getString("systemCode"));			
		obj.setBillType(row.getString("billType"));
		obj.setBillTypeName(row.getString("billTypeName"));
		obj.setOrderDate(row.getDate("orderDate"));
		obj.setState(row.getString("state"));
		obj.setStatus(row.getString("status"));

		obj.setUnitCode(row.getString("unitCode"));
		obj.setUnitName(row.getString("unitName"));
		obj.setUnitType(row.getString("unitType"));
		
		obj.setComments(row.getString("comments"));

		obj.setcUsercode(row.getString("cUsercode"));
		obj.setcUsername(row.getString("cUsername"));
		obj.setcDatetime(row.getDateTime("cDatetime"));
		obj.settUsercode(row.getString("tUsercode"));
		obj.settUsername(row.getString("tUsername"));
		obj.settDatetime(row.getDateTime("tDatetime"));
		obj.seteUsercode(row.getString("eUsercode"));
		obj.seteUsername(row.getString("eUsername"));
		obj.seteDatetime(row.getDateTime("eDatetime"));
		
		obj.setsCreatorFID(row.getString("sCreatorFID"));
		
		obj.setUserId(row.getString("userId"));

		obj.setEnterpriseNo(row.getString("enterpriseNo"));
		obj.setEnterpriseName(row.getString("enterpriseName"));
		
		obj.setVersion(row.getInteger("version"));
		
		obj.setRowState(row.getState());

		return obj;
	}
	*/
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IFileUnitService#save(com.alibaba.fastjson.JSONObject)
	 */
	@Transactional
	
	@Override
	public int save(JSONObject object) throws Throwable {
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			FileUnit obj = JSON.toJavaObject(javaObject, FileUnit.class);
			if(RowState.NEW.equals(obj.getRowState())){
//				obj.setcUsercode(SecurityService.getCurrentPerson().getsCode());
//				obj.setcUsername(SecurityService.getCurrentPerson().getsName());
//				obj.setcDatetime(SystemService.getCurrentDateTime());
				dao.insert(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
//				obj.seteUsercode(SecurityService.getCurrentPerson().getsCode());
//				obj.seteUsername(SecurityService.getCurrentPerson().getsName());
//				obj.seteDatetime(SystemService.getCurrentDateTime());
				dao.update(obj);
			}
		}
		return 0;
	}
	
	/* (non-Javadoc)
	 * @see com.iune.erp.baseinfo.aaa#queryJDBC(com.alibaba.fastjson.JSONObject)
	 */
	/* (non-Javadoc)
	 * @see com.iune.erp.inventory.IFileUnitService#queryJDBC(com.alibaba.fastjson.JSONObject)
	 */
	@Override
	public JSONObject queryJDBC(JSONObject data) throws Throwable{
		return dao.queryJDBC(data);
	}

}
